//
//  ModelController.swift
//  PageBasedApplicationDemo
//
//  Created by Ravi Shankar on 13/08/14.
//  Copyright (c) 2014 Ravi Shankar. All rights reserved.
//

import UIKit

/*
  A controller object that manages a simple model -- a collection of month names.

  The controller serves as the data source for the page view controller; it therefore implements pageViewController:viewControllerBeforeViewController: and pageViewController:viewControllerAfterViewController:.
  It also implements a custom method, viewControllerAtIndex: which is useful in the implementation of the data source methods, and in the initial configuration of the application.

 ///#XcodeTranslater:实际上没有必要提前为每个页面创建视图控制器——这样做确实会带来不必要的开销。给定数据模型，这些方法根据需要创建、配置和返回一个新的视图控制器。
  There is no need to actually create view controllers for each page in advance -- indeed doing so incurs unnecessary overhead. Given the data model, these methods create, configure, and return a new view controller on demand.
  */

class ModelController: NSObject, UIPageViewControllerDataSource {
  let pageData: NSArray = ["I'm as proud of what we don't do as I am of what we do - Steve Jobs", "That's one small step for man, one giant leap for mankind - Neil Armstrong", "An ant on the move does more than a dozing ox - Lao Tzu", "I mean, it's impossible But that's exactly what we've tried to do - Jonathan Ive"]
  override init() {
    super.init()
    // Create the data model.
  }

  func viewControllerAtIndex(index: Int, storyboard: UIStoryboard) -> DataViewController? {
    // Return the data view controller for the given index.
    if (pageData.count == 0) || (index >= pageData.count) {
      return nil
    }

    // Create a new view controller and pass suitable data.
    let dataViewController = storyboard.instantiateViewController(withIdentifier: "DataViewController") as! DataViewController
    dataViewController.dataObject = pageData[index] as AnyObject
    return dataViewController
  }

  func indexOfViewController(viewController: DataViewController) -> Int {
    // Return the index of the given data view controller.
    // For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
    return pageData.index(of: viewController.dataObject!)
  }

  // MARK: - Page View Controller Data Source

  func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    var index = indexOfViewController(viewController: viewController as! DataViewController)
    if (index == 0) || (index == NSNotFound) {
      return nil
    }

    index -= 1
    return viewControllerAtIndex(index: index, storyboard: viewController.storyboard!)
  }

  func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    var index = indexOfViewController(viewController: viewController as! DataViewController)
    if index == NSNotFound {
      return nil
    }

    index += 1
    if index == pageData.count {
      return nil
    }
    return viewControllerAtIndex(index: index, storyboard: viewController.storyboard!)
  }
}
